package com.beyond.pm.ui.main

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.hilt.navigation.compose.hiltViewModel
import com.beyond.common.local.LocalDarkMode
import com.beyond.common.local.LocalSystemPrimaryModel
import com.beyond.common.provider.ThemeSettingsProvider
import com.beyond.flow.collect.collectState
import com.beyond.pm.component.MySnackBar
import com.beyond.pm.component.WaitingDialog
import com.beyond.pm.local.LocalLanguageState
import com.beyond.pm.local.loadStatusState
import com.beyond.pm.local.snackStatusState
import com.beyond.pm.nav.AppNavHost
import com.beyond.pm.provider.LanguageSettingsProvider
import com.beyond.pm.ui.launch.SplashPage
import com.beyond.theme.ProjectManageTheme
import dagger.hilt.android.AndroidEntryPoint
import java.util.Locale

/**
 * @author chen
 * @time 2023/5/31 11:20
 * @name MainActivity.kt
 * @class describe
 */
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("RememberReturnType")
    override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        setContent {
            // https://juejin.cn/post/7016897391315927076 导航栏
            ThemeSettingsProvider {
                LanguageSettingsProvider {
                    val currentLocale = LocalConfiguration.current
                    val localeLanguage = LocalLanguageState.current
                    val locale = Locale(localeLanguage)
                    currentLocale.setLocale(locale)
                    resources.updateConfiguration(currentLocale, resources.displayMetrics)
                    AppContent()
                }
            }
        }
    }

    @Composable
    private fun AppContent() {
        ProjectManageTheme(
            darkThemeInt = LocalDarkMode.current,
            systemScheme = LocalSystemPrimaryModel.current
        ) {
            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                var isSplash by rememberSaveable { mutableStateOf(true) }
                if (isSplash) {
                    SplashPage { isSplash = false }
                } else {
                    MainContent()
                }
            }
        }
    }

    @Composable
    private fun MainContent(viewModel: MainViewModel = hiltViewModel()) {

        val snackBarHostState by snackStatusState

        val lifecycleOwner = LocalLifecycleOwner.current
        // val loginStatusState = mutableStateOf(false)
        // var isLogin by  loginStatusState
        // 使用这个 mutableStateOf  会导致  isLogin先执行false 再执行true 导致login及viewmodel 未及时销毁

        val isLogin =
            rememberSaveable { mutableStateOf(viewModel.container.uiStateFlow.value.isLogin) }
        val isLoading by loadStatusState
        LaunchedEffect(Unit) {
            viewModel.container.run {
                uiStateFlow.collectState(lifecycleOwner) {
                    collectPartial(MainUiState::isLogin) {
                        isLogin.value = it
                    }
                }
            }
        }
        // 显示加载对话框
        AppNavHost(isLogin.value)
        Box(modifier = Modifier.fillMaxSize()) {
            MySnackBar(
                snackBarHostState = snackBarHostState,
                onDismiss = { snackBarHostState.currentSnackbarData?.dismiss() },
                modifier = Modifier
                    .padding(top = 50.dp)
                    .align(Alignment.TopCenter)
            )
        }
        WaitingDialog(
            visible = isLoading
        )
    }
}
